package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
//    @Autowired
//    private UserMapper userMapper;
    @Autowired
    private UserService userService;

    @GetMapping//查询所有记录
    public List<User> index() {
        return userService.list();
    }

    @PostMapping//添加或者修改
    public boolean save(@RequestBody User user) {

        return userService.saveUser(user);
    }

    @DeleteMapping("/{id}")//删除
    public Boolean delete(@PathVariable Integer id) {
        return userService.removeById(id);
    }

//    @GetMapping("/{id}")//查询单条记录
//    public Integer find(@PathVariable Integer id) {
//        return userMapper.find(id);
//    }

//    @GetMapping("/page")//分页查询
//    public Map<String, Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String username) {
//        pageNum = (pageNum - 1) * pageSize;
//        List<User> data = userMapper.selectPage(pageNum, pageSize, username);
//        Integer total = userMapper.selectTotal(username);
//        Map<String, Object> res = new HashMap<>();
//        res.put("data", data);
//        res.put("total", total);
//        return res;
//    }
        @GetMapping("/page")//分页查询
        public IPage<User> findPage(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize,
                                    @RequestParam String username,
                                    @RequestParam String nickname,
                                    @RequestParam String address) {
            IPage<User> page=new Page<>(pageNum,pageSize);
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("username",username);
            queryWrapper.like("nickname",nickname);
            queryWrapper.like("address",address);
            return userService.page(page, queryWrapper);
        }
}
